package com.example.admin.goodnews.fragment.QingSongYiXia;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.activity.SecondaryPageActivity;
import com.example.admin.goodnews.adapter.DuanZiAdapter;
import com.example.admin.goodnews.adapter.XRecyclerViewAdapter;
import com.example.admin.goodnews.entity.DuanZiDataEntity;
import com.example.admin.goodnews.entity.GoodNewsDataEntity;
import com.example.admin.goodnews.entity.ShiPinDataEntity;
import com.example.admin.goodnews.fragment.XinWenFenLei.CaiJingJiaoDianFragment;
import com.example.admin.goodnews.utils.GetCurrentTimeUtil;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 段子页面
 * Created by admin on 2016/10/14.
 */

public class DuanZiFragment extends Fragment {
    private XRecyclerView xrecyclerview;
    private List<DuanZiDataEntity.ShowapiResBodyBean.ContentlistBean> datalists = new ArrayList<>();// 适配器需要的数据列表
    private List<DuanZiDataEntity.ShowapiResBodyBean.ContentlistBean> lists = new ArrayList<>();// 存放临时数据的列表
    private LinearLayoutManager linearLayoutManager;// 布局管理器
    private DuanZiAdapter duanZiAdapter;// 数据适配器
    String time = GetCurrentTimeUtil.getCurrentTime();// 获取系统当前时间
    private int page = 1;// 页数，用于上拉加载和下拉刷新

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tongyongbuju, null);
        xrecyclerview = (XRecyclerView) view.findViewById(R.id.xrecyclerview_tongyongbuju);// 用于加载数据的列表
        // 设置布局管理器
        linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        xrecyclerview.setLayoutManager(linearLayoutManager);
        // 下拉刷新和上拉加载
        xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
        getData();
        return view;
    }

    private void getData() {
        String param = NetWorkDataUrl.DUANZI_PARAM1 + page + NetWorkDataUrl.DUANZI_PARAM2 + time + NetWorkDataUrl.DUANZI_PARAM3;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.DUANZI_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    lists = gson.fromJson(result, DuanZiDataEntity.class).getShowapi_res_body().getContentlist();
                    if (lists.size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    if (page == 1) {
                        datalists.clear();
                        datalists.addAll(lists);
                        duanZiAdapter = new DuanZiAdapter(getActivity(), datalists);// 初始化适配器
                        xrecyclerview.setAdapter(duanZiAdapter);// 给RecyclerView设置适配器
                        xrecyclerview.refreshComplete();
                        lists.clear();
                    } else {
                        datalists.addAll(lists);
                        duanZiAdapter.notifyDataSetChanged();
                        xrecyclerview.loadMoreComplete();
                        lists.clear();
                    }
                    break;
            }
        }
    };


}
